//## 게시판 정보 저장 ####################
function fnSave()
{
	if (!$("#titles").val())
	{
		alert("제목을 바르게 입력해 주세요!");
		$("#titles").focus();
		return;
	}
	var param  = "titles="+encodeURIComponent($("#titles").val());
		param += "&contents="+encodeURIComponent($("#contents").val());
		param += "&startDt="+$("#startDt").val();
		param += "&endDt="+$("#endDt").val();
		param += "&flag="+$("#flag").val();
		param += "&seq="+$("#seq").val();
		if ($("#noticeFlag").is(":checked")) param += "&noticeFlag=Y";


	$.ajax({
		url: '/action/board/save.asp',
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				grid01List();
				fnReset();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 게시판 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/board/list.asp";
	var param = "findTitles="+encodeURIComponent($("#findTitles").val());
		param += "&findContents="+encodeURIComponent($("#findContents").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Board").find("BbsList").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var noticeFlag	= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("noticeFlag").text();
		var titles			= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("titles").text();
		var userNm		= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("userNm").text();
		var wdate			= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("wdate").text();
		var hit				= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("hit").text();		
		var terms			= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("terms").text();		
		var flag				= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("flag").text();
		var seq				= $(G_grid01Rslt).find("Board").find("BbsList").eq(j).find("seq").text();

		ji = j+1;
		
		var showFlag = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";
		
		var nFlag = "&nbsp;";
		if (noticeFlag == "Y") {
			nFlag = "<b>공지</b>";
			titles = "<b>" + titles + "</b>";
		}

		grid01Array[0] = ji;
		grid01Array[1] = nFlag;
		grid01Array[2] = titles;
		grid01Array[3] = userNm;
		grid01Array[4] = wdate;
		grid01Array[5] = hit;
		grid01Array[6] = terms;
		grid01Array[7] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,left,,,,,", "fnSetBoard('"+seq+"')", boldLine);
	}
}


//## 게시판 상세정보 표시 ####################
function fnSetBoard(seq)
{
	var valUrl = "/action/board/view.asp";
	var param = "seq="+seq;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			$("#seq").val($(xml).find("seq").text());
			$("#titles").val($(xml).find("titles").text());
			$("#contents").val($(xml).find("contents").text());
			$("#startDt").val($(xml).find("startDt").text());
			$("#endDt").val($(xml).find("endDt").text());
			$("#flag").val($(xml).find("flag").text());
			if ($(xml).find("noticeFlag").text() == "Y") $("#noticeFlag").attr("checked",true);
			if ($(xml).find("updateDt").text())
			{
				$("#accessInfo").val("최종수정 : "+$(xml).find("userNm").text()+" / "+$(xml).find("updateDt").text());
			}
			else
			{
				$("#accessInfo").val("최종수정 : "+$(xml).find("userNm").text()+" / "+$(xml).find("issueDt").text());
			}

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 게시판 정보 Reset 기능
function fnReset()
{
	$("#seq").val("");
	$("#titles").val("");
	$("#contents").val("");
	$("#startDt").val("");
	$("#endDt").val("");
	$("#flag").val("Y");
	$("#accessInfo").val("");
	$("#noticeFlag").attr("checked",false);
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findTitles").val("");
	$("#findContents").val("");
}